package com.jason.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.jason.system.common.ResultEntity;
import com.jason.system.common.util.EasyPoiUtil;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.Order;
import com.jason.system.entity.SolveOrder;
import com.jason.system.service.SolveOrderService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/4/1 21:23
 */

@RestController
@RequestMapping("/api/solveOrder")
@Api(value = "/solveOrder", tags = "完成工单接口")
public class SolveOrderController {

  private static final Logger logger = LoggerFactory.getLogger(SolveOrderController.class);

  @Autowired
  private SolveOrderService solveOrderService;


  @ApiOperation(value = "获取登单信息",notes = "获取所有完成登单信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "page",value = "页数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "size",value = "每页个数",required = true,dataType = "Integer")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "获取所有登单信息成功"),
          @ApiResponse(code = 201,message = "获取所有登单信息失败")
  })
  @GetMapping("/getAll")
  public JSONObject getAllSolveOrders(@RequestParam("page") Integer page ,@RequestParam("size") Integer size){
    logger.info("==>>>SolveOrderController==>>>进行路径：/api/solveOrder/getAll.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    Pager<SolveOrder> solveOrders = solveOrderService.getSolveOrders(page,size);
    logger.info("==>>>获取SolveOrders对象==>>>: {}",solveOrders);
    if (solveOrders!=null) {
      r.setData(solveOrders);
      r.setCode(200);
      r.setMessage("获取所有登单信息成功");
    }else {
      r.setCode(201);
      r.setMessage("获取所有登单信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "添加登单信息",notes = "添加登单信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "order",value ="派单信息",required = true,dataType = "Order"),
          @ApiImplicitParam(paramType = "query",name = "customerName",value = "客户姓名",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "register",value = "登记人",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "solveTime",value = "解决时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "maintainer",value = "处理人",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "material",value ="耗材信息",required = true,dataType = "Material"),
          @ApiImplicitParam(paramType = "query",name = "status",value = "处理结果",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "reason",value = "具体原因",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "solution",value = "解决方法",required = true,dataType = "String")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "添加登单信息成功"),
          @ApiResponse(code = 201,message = "添加登单信息失败")
  })
  @PostMapping("/insert")
  public JSONObject insertSolveOrder(@RequestBody SolveOrder solveOrder){
    logger.info("==>>>SolveOrderController==>>>进行路径：solveOrder:{}",solveOrder);
    logger.info("==>>>SolveOrderController==>>>进行路径：/api/solveOrder/insert.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>SolveOrderController==>>>正在添加SolveOrder中.....");
    try {
      solveOrderService.insertSolveOrder(solveOrder);
      logger.info("==>>>SolveOrderController==>>>添加SolveOrder完成.....");
      r.setCode(200);
      r.setMessage("添加SolveOrder成功");
    }catch (Exception e){
      logger.info("==>>>SolveOrderController==>>>添加SolveOrder失败.....");
      r.setCode(201);
      r.setMessage("添加SolveOrder失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "更新登单信息",notes = "更新登单信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "order",value ="派单信息",required = true,dataType = "Order"),
          @ApiImplicitParam(paramType = "query",name = "customerName",value = "客户姓名",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "register",value = "登记人",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "solveTime",value = "解决时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "maintainer",value = "处理人",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "material",value ="耗材信息",required = true,dataType = "Material"),
          @ApiImplicitParam(paramType = "query",name = "status",value = "处理结果",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "reason",value = "具体原因",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "solution",value = "解决方法",required = true,dataType = "String")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "更新登单信息成功"),
          @ApiResponse(code = 201,message = "更新登单信息失败")
  })
  @PostMapping("/update")
  public JSONObject updateSolveOrder(@RequestBody SolveOrder solveOrder){
    logger.info("==>>>SolveOrderController==>>>进行路径：/api/solveOrder/update.....");
    logger.info("==>>>SolveOrderController==>>>进行路径：solveOrder:{}",solveOrder);
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>SolveOrderController==>>>正在更新SolveOrder中.....");
    try {
      solveOrderService.updateSolveOrder(solveOrder);
      logger.info("==>>>SolveOrderController==>>>更新SolveOrder完成.....");
      r.setCode(200);
      r.setMessage("更新SolveOrder成功");
    }catch (Exception e){
      logger.info("==>>>SolveOrderController==>>>更新SolveOrder失败.....");
      r.setCode(201);
      r.setMessage("更新SolveOrder失败");
      e.printStackTrace();
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "更新登单信息状态",notes = "更新登单信息状态")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "soId",value = "登单id",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "status",value = "登单状态",required = true,dataType = "Integer")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "更新登单信息状态成功"),
          @ApiResponse(code = 201,message = "更新登单信息状态失败")
  })
  @PostMapping("/updateStatus")
  public JSONObject updateOrderStatus(Integer soId , Integer status){
    logger.info("==>>>SolveOrderController==>>>进行路径：/api/SolveOrder/updateStatus.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>SolveOrderController==>>>正在更新SolveOrder状态中.....");
    try {
      solveOrderService.updateSolveOrderStatus(soId,status);
      logger.info("==>>>SolveOrderController==>>>更新SolveOrder完成.....");
      r.setCode(200);
      r.setMessage("更新SolveOrder状态成功");
    }catch (Exception e){
      logger.info("==>>>SolveOrderController==>>>更新SolveOrder失败.....");
      r.setCode(201);
      r.setMessage("更新SolveOrder状态失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "删除登单信息",notes = "删除登单信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "soId",value = "登单id",required = true,dataType = "Integer"),
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "删除登单信息状态成功"),
          @ApiResponse(code = 201,message = "删除登单信息状态失败")
  })
  @PostMapping("/delete/{soId}")
  public JSONObject deleteSolveOrder(@PathVariable("soId") Integer soId){
    logger.info("==>>>SolveOrderController==>>>进行路径：/api/SolveOrder/delete.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>SolveOrderController==>>>正在删除SolveOrder中.....");
    boolean result = solveOrderService.deleteSolveOrderById(soId);
    if (result){
      r.setCode(200);
      r.setMessage("删除SolveOrder成功");
      logger.info("==>>>SolveOrderController==>>>删除SolveOrder完成.....");
    }else {
      r.setCode(201);
      r.setMessage("删除SolveOrder失败");
      logger.info("==>>>SolveOrderController==>>>删除SolveOrder失败.....");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "批量删除登单信息",notes = "批量删除登单信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "soId",value = "登单ids,以逗号分割",required = true,dataType = "String"),
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "批量删除登单信息成功"),
          @ApiResponse(code = 201,message = "批量删除登单信息失败")
  })
  @PostMapping("/deleteSolveOrders")
  public JSONObject deleteSolveOrders(String soIds){
    logger.info("==>>>SolveOrderController==>>>进行路径：/api/SolveOrder/deleteSolveOrders.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>SolveOrderController==>>>正在批量删除SolveOrders中.....");
    boolean result = solveOrderService.deleteSolveOrders(soIds);
    if (result){
      r.setCode(200);
      r.setMessage("批量删除SolveOrders成功");
      logger.info("==>>>SolveOrderController==>>>批量删除SolveOrders完成.....");
    }else {
      r.setCode(201);
      r.setMessage("批量删除SolveOrders失败");
      logger.info("==>>>SolveOrderController==>>>批量删除SolveOrders失败.....");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @PostMapping("/search")
  public JSONObject searchOrders(Integer page , Integer size , Integer sId){
    logger.info("==>>>SolveOrderController==>>>进行路径：/api/solveOrder/search.....");
    logger.info("==>>>SolveOrderController==>>>sId:{}",sId);
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>SolveOrderController==>>>正在查询solveOrders中.....");
    Pager<SolveOrder> solveOrderPager = solveOrderService.searchSolveOrders(page,size,sId);
    logger.info("==>>>SolveOrderController==>>>orders:{}",solveOrderPager);
    if (solveOrderPager!=null){
      logger.info("==>>>SolveOrderController==>>>查询solveOrders完成.....");
      r.setCode(200);
      r.setMessage("查询登单信息成功");
      r.setData(solveOrderPager);
    }else {
      logger.info("==>>>SolveOrderController==>>>查询solveOrders失败.....");
      r.setCode(201);
      r.setMessage("查询登单信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }


  @ApiOperation(value = "导出excel",notes = "根据时间段导出登单信息至excel")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "startTime",value = "起始时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "endTime",value = "结束时间",required = true,dataType = "Date")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "导出excel成功"),
          @ApiResponse(code = 201,message = "导出excel失败")
  })
  @PostMapping("/downLoad")
  public JSONObject downLoad(HttpServletResponse response, List<SolveOrder> solveOrderList,Date endTime)  throws IOException {
    logger.info("==>>>SolveOrderController==>>>进行路径：/api/SolveOrder/downLoad.....");
    //获取月份
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    Calendar cld = Calendar.getInstance();
    cld.setTime(endTime);
    Integer month = cld.get(Calendar.MONTH);
    String fileName = "网络维护"+month+"月汇总";
//    //根据获取月份来查找数据，导入到excel中
//    logger.info("==>>>SolveOrderController==>>>根据时间段将登单信息导入对应的月份excel表中.....");
//    List<SolveOrder> solveOrderList = solveOrderService.getAllSolveOrderByTime(startTime,endTime);
    try {
      logger.info("==>>>SolveOrderController==>>>使用easyPoi工具类将登单信息导入至excel中...");
      EasyPoiUtil.defaultExport(solveOrderList,fileName,"network",fileName+".xlsx",
              SolveOrder.class,response);
      r.setCode(200);
      r.setMessage("导入至excel中成功");
    }catch (Exception e){
      logger.info("==>>>SolveOrderController==>>>使用easyPoi工具类将登单信息导入至excel失败:{}",e.getMessage());
      r.setCode(201);
      r.setMessage("导入至excel中失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }
}
